package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pojo.DoctorWork;
import pojo.WorkPlan;
import service.WorkPlanService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(value = "工作日程管理", description = "医院工作日程相关接口")
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/workPlan")
public class WorkPlanController {

    @Autowired
    private WorkPlanService workPlanService;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @ApiOperation(value = "查询所有工作日程", notes = "获取所有工作日程信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回日程列表", response = WorkPlan.class, responseContainer = "List")
    })
    @GetMapping("/findAll")
    public ResponseEntity<List<WorkPlan>> findAll() {
        return ResponseEntity.ok(workPlanService.findAll());
    }

    @ApiOperation(value = "根据日期查询日程", notes = "通过日期获取单个工作日程信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回日程", response = WorkPlan.class),
            @ApiResponse(code = 404, message = "未找到对应日程")
    })
    @GetMapping("/findByDay/{day}")
    public ResponseEntity<WorkPlan> findByDay(@PathVariable String day) {
        try {
            Date date = dateFormat.parse(day);
            WorkPlan plan = workPlanService.findByDay(date);
            return plan != null ? ResponseEntity.ok(plan) : ResponseEntity.notFound().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @ApiOperation(value = "新增工作日程", notes = "创建一个新的工作日程")
    @ApiResponses({
            @ApiResponse(code = 201, message = "日程创建成功"),
            @ApiResponse(code = 400, message = "无效的日程数据"),
            @ApiResponse(code = 409, message = "该日期日程已存在")
    })
    @PostMapping("/addWorkPlan")
    public ResponseEntity<Void> addWorkPlan(@RequestBody WorkPlan workPlan) {
        // 检查日期是否已存在
        if (workPlanService.findByDay(workPlan.getDay()) != null) {
            return ResponseEntity.status(HttpStatus.CONFLICT).build();
        }
        workPlanService.addWorkPlan(workPlan);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @ApiOperation(value = "更新日程信息", notes = "根据日期更新工作日程")
    @ApiResponses({
            @ApiResponse(code = 200, message = "日程信息更新成功"),
            @ApiResponse(code = 404, message = "未找到对应日程")
    })
    @PostMapping("/updateWorkPlan")
    public ResponseEntity<Void> updateWorkPlan(@RequestBody WorkPlan workPlan) {
        int success = workPlanService.updateWorkPlan(workPlan);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "删除日程", notes = "根据日期删除工作日程")
    @ApiResponses({
            @ApiResponse(code = 200, message = "日程删除成功"),
            @ApiResponse(code = 404, message = "未找到对应日程")
    })
    @DeleteMapping("/deleteWorkPlan/{day}")
    public ResponseEntity<Void> deleteWorkPlan(@PathVariable String day) {
        try {
            Date date = dateFormat.parse(day);
            int success = workPlanService.deleteWorkPlan(date);
            return success > 0 ?
                    ResponseEntity.ok().build() :
                    ResponseEntity.notFound().build();
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @RequestMapping("/getDoctorWorks/{week}")
    @ResponseBody
    public List<DoctorWork> getDoctorWorks(@PathVariable String week){
        return workPlanService.getWorkPlanByWeek(week);
    }
}